﻿//using System;
//using System.Data;
//using System.Configuration;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
//using suanpan.API.Components.Data;
//using suanpan.API.Components.Contracts.Xml;
//using suanpan.API.Components.Contracts;
//using suanpan.API.Components.Core;
//using System.Collections;
//using System.Text.RegularExpressions;
//using suanpan.API.Components.Exceptions;

//namespace suanpan.API.Components.Providers
//{
//    public class ChessGameProvider
//    {

//        #region Private Mapping Methods
//        private ChessMove MapPersistedMoveToChessMove(game_move persistedMove)
//        {
//            ChessMove move = new ChessMove();
//            MapPersistedMoveToChessMove(persistedMove, move);
//            return move;
//        }

//        private void MapPersistedMoveToChessMove(game_move persistedMove, ChessMove move)
//        {
//            move.From = persistedMove.from_square;
//            move.To = persistedMove.to_square;
//            move.Piece = ChessGMLMapper.GetChessPiece(persistedMove.piece);
//            move.Player = (persistedMove.number % 2 == 1) ? ChessSide.White : ChessSide.Black;
//            move.Capture = persistedMove.taking_move;
//            move.Check = persistedMove.check_move;
//        }

//        private ChessGame MapPersistedGameToChessGame(game persistedGame)
//        {
//            ChessGame chessGame = new ChessGame();
//            MapPersistedGameToChessGame(persistedGame, chessGame);
//            return chessGame;
//        }

//        private void MapPersistedGameToChessGame(game persistedGame, ChessGame chessGame)
//        {
//            chessGame.Id = persistedGame.game_id;

//            chessGame.Year = persistedGame.chess_event.date.Year;
//            chessGame.EventInfo = new ChessEventInfo(persistedGame.chess_event.location, persistedGame.chess_event.title);

//            chessGame.WhiteSidePlayer = new ChessPlayer(ChessSide.White, persistedGame.player.handle, string.Empty);
//            chessGame.BlackSidePlayer = new ChessPlayer(ChessSide.Black, persistedGame.player1.handle, string.Empty);

//            int totalMoves = persistedGame.game_moves.Count;
//            int max = Convert.ToInt32(Math.Floor((double)totalMoves / 2) * 2);
//            for ( int i = 0; i < max; i += 2 )
//            {
//                ChessMove whiteMove = MapPersistedMoveToChessMove(persistedGame.game_moves[i]);
//                ChessMove blackMove = MapPersistedMoveToChessMove(persistedGame.game_moves[i + 1]);
//                ChessTurn turn = new ChessTurn(whiteMove, blackMove);
//                chessGame.Moves.Add(turn);
//            }

//            if ( totalMoves % 2 == 1 )
//            {
//                ChessMove whiteMove = MapPersistedMoveToChessMove(persistedGame.game_moves[totalMoves - 1]);
//                chessGame.Moves.Add(new ChessTurn(whiteMove, null));
//            }
//        }

//        private game GetPersistedGameByTitle(string gameTitle)
//        {
//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = db.games.SingleOrDefault(p => p.title == gameTitle.ToLower());
//            if ( persistedGame == null )
//                throw new ResourceNotFoundException("No game found for game title = " + gameTitle);
//            return persistedGame;
//        }

//        #endregion

//        public ChessMove MoveGet(string gameTitle, string moveNotation)
//        {
//            int moveNumber = ChessGMLMapper.GetMoveNumber(moveNotation);

//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = GetPersistedGameByTitle(gameTitle);
//            game_move persistedMove = persistedGame.game_moves.SingleOrDefault(p => p.number == moveNumber);

//            if ( persistedMove == null )
//                throw new ResourceNotFoundException(string.Format("Invalid move request for at {0}.  The current move for game '{1}' is {2}.", moveNotation, gameTitle, ChessGMLMapper.GetMoveNotation(persistedGame.game_moves.Count + 1)));

//            return MapPersistedMoveToChessMove(persistedMove);
//        }

//        public bool MoveAdd(string gameTitle, string turn, ChessMove move)
//        {
//            ChessGMLMapper.ValidateSquareNotation(move.From);
//            ChessGMLMapper.ValidateSquareNotation(move.To);

//            int moveNumber = ChessGMLMapper.GetMoveNumber(turn);
//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = GetPersistedGameByTitle(gameTitle);

//            if ( moveNumber != persistedGame.game_moves.Count + 1 )
//                throw new BadRequestException(string.Format("{0} is not the current move. {1} is the current move.", turn, ChessGMLMapper.GetMoveNotation(persistedGame.game_moves.Count + 1)));

//            game_move persistedMove = new game_move();
//            persistedMove.game_id = persistedGame.game_id;
//            persistedMove.from_square = move.From;
//            persistedMove.to_square = move.To;
//            persistedMove.number = moveNumber;
//            persistedMove.taking_move = move.Capture;
//            persistedMove.check_move = move.Check;
//            persistedMove.piece = ChessGMLMapper.GetChessGMLPieceValue(move.Piece)[0];

//            db.game_moves.InsertOnSubmit(persistedMove);
//            db.SubmitChanges();

//            return true;
//        }

//        public ChessGame GameGet(int gameId)
//        {
//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = db.games.SingleOrDefault(p => p.game_id == gameId);
//            if ( persistedGame == null )
//                throw new ResourceNotFoundException("No game found for gameid = " + gameId.ToString());

//            return MapPersistedGameToChessGame(persistedGame);
//        }

//        public List<ChessGame> GamesGet()
//        {
//            RestChessDataContext db = new RestChessDataContext();
            
//            List<ChessGame> games = new List<ChessGame>();

//            foreach ( game persistedGame in db.games )
//                games.Add(MapPersistedGameToChessGame(persistedGame));

//            return games;
//        }

//        public ChessGame GameGet(string gameTitle)
//        {

//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = GetPersistedGameByTitle(gameTitle);

//            return MapPersistedGameToChessGame(persistedGame);
//        }

//        public void GameAdd(ChessGame newGame)
//        {
//            RestChessDataContext db = new RestChessDataContext();

//            game persistedGame = new game();
//            persistedGame.chess_event_id = 1;
//        }

        
        
//    }
//}
